|
Impact AIT |
Return to Index
›Data Exchange - TCPIP -
VB6
›Data Exchange - TCPIP - VB6 - Receive Data
Transfer data to and from a Datalogic device
Notes:
The default AIT root distribution folder for this application is:
/Datalogic/IMPACT/AIT/Data Exchange/TCPIP
Note: If you receive
the following error message when you attempt to run any of the included sample
files,
Component 'MSWINSCK.OCX' or one
of its dependencies not correctly registered: a file is missing or invalid
you need to install the Visual Basic 6.0 Run-time Files that include the file
MSWINSCK.OCX.
Part 2 – Create a listener to receive data from the device
Modify the vision program
Modify the Visual Basic Project
Modify the vision program created in Part 1 (Go to Part 1)
1.
Start
VPM and open the vision program file modified in Part 1 (Light Bulb Inspection
TCPIP.vp). (Note: The completed sample program is in the AIT distribution
subfolder /VisualBasic6/Example1/IMPACT Files/Light Bulb Inspection TCPIP.vp.)
2.
Add
a new task to the program and rename it to Send
TCPIP Message.
3.
Select
None for the trigger event. See
example
4.
Add
a TCP/IP Out tool from the Data tool group.
5.
Select
the task and click the Add Input button. In the TCP/IP Out tool, select the
Input String property. This will promote the tool's property to a task input. See example
6.
Select
the TCPIP Event Received task.
7.
Steps
8 through 10 will set up a Data Tool to send the parsed incoming IP address
into a TCP/IP Out tool so a response can be sent to the correct Datalogic device.
8.
Add
a Data Set tool (from the Data tool group) after the Basic tool named Parse Incoming String.
9.
Set
the Data Set tool's Data Type to String and rename it to Change IP Address.
10.
Click
the Create Patch icon in the Data Set tool's value property. In the Basic tool
(Parse Incoming String) select
the property named ip_address_string.
See example
11.
Click
the Create Patch icon in the Change IP
Address Data Set tool's
12.
Steps
12 through 14 will set up a Data Tool to send the parsed incoming Port Number
into a TCP/IP Out tool so a response can be sent to the correct device port.
13.
Add
a Data Set tool (from the Data tool group) below the first Data Set tool. Set
the tool's Data Type to Integer and rename it to Change Port Number. See example
14.
Click
the Create Patch icon in the Change Port
Number Data Set tool's property value. In the Basic tool (Parse Incoming String) select the
property named port_number_int.
See example
15.
Click
the Create Patch icon in the Data Set tool's
16.
Add
a Call Task tool from the Programming tool group below the second Data Set tool
(named Change Port Number).
Rename it to Send Response Message.
17.
In
the Call Task tool's Task value field, select Light Bulb Inspection TCPIP.Send TCPIP Message from the drop
down list. See example
18.
In
the Call Task tool's Input String value field, enter the string "This is
the response message" without the quotes. This string will be sent back to
the received IP address and Port Number.
19.
Save
the file and unlock VPM. Click the online/offline button in VPM to put the
device online.
Modify the Visual Basic project created in Part 1 (Go to Part 1)
1.
Start
Microsoft Visual Basic 6.0. (Note: This sample code is in the AIT distribution
subfolder /VisualBasic6/Example1/Project.)
2.
From
the project explorer, select the project Impact_TCPIP.
3.
Add
a new Winsock control to the form and rename it to Data From Device.
4.
Add
a new label control to the form and rename it to Received_Data.
5.
Delete
the value in Received_Data’s caption.
6.
Double
click on the form and replace all existing code with the following code. (You
may select and copy the code from this page.)
Private Sub Connect_Click()
'Set the IP address and port number of the Datalogic device
Data_To_Device.Close
Data_To_Device.RemoteHost = IPAddress.Text
Data_To_Device.RemotePort = CInt(PortNumber.Text)
'The device has to be online before it can receive any TCPIP
events
If Data_To_Device.State = sckClosed Then
Data_To_Device.Connect
End If
End Sub
Private Sub Data_To_Device_Connect()
'Send IP Address and port number information to
the device
Data_To_Device.SendData Data_To_Device.LocalIP
& "," & Data_From_Device.LocalPort & vbCrLf
End Sub
Private Sub Form_Load()
'Set the local port number to listen on
Data_From_Device.LocalPort = 5150
'Display the IP address and port number of this
PC
Me.Caption = "Local IP Address = "
& Data_To_Device.LocalIP & ", Port Number = " &
Data_From_Device.LocalPort
'Start listening for data
Data_From_Device.Listen
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Close the ports
Data_To_Device.Close
Data_From_Device.Close
End Sub
Private Sub Data_From_Device_ConnectionRequest(ByVal requestID As Long)
'A connection request had been received, close
the port and accept the connection
If Data_From_Device.State <> skclosed Then
Data_From_Device.Close
End If
Data_From_Device.Accept requestID
End Sub
Private Sub Data_From_Device_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
'Extract the incoming data
Data_From_Device.GetData strData
Received_Data.Caption = strData
End Sub
7.
Save
the project.
8.
Make
an executable file from the project then run the executable. What does the program User
Interface look like?
When you click the Connect button, the Visual Basic program sends the displayed IP Address and Port Number to the Datalogic device. When the device receives the TCPIP message, it runs the TCPIP Event Received task in the vision program Light Bulb Inspection TCPIP.
The TCPIP Event Received task sets the IP Address and Port Number on the TCP/IP Out tool to what was received, then runs the TCP/IP Out tool. This sends the message contained in the tool's Input String property back to the Visual Basic program. This program is listening on the displayed port for a response. When the message is received, the Visual Basic program extracts the message string and displays it in the Caption field of the Received_Data label control.
©2003-2013 Datalogic Automation Inc. All Rights Reserved